//
//  XYBottomMenuView.swift
//  XYReadBook
//
//  Created by fox on 2017/9/9.
//  Copyright © 2017年 tsaievan. All rights reserved.
//

import UIKit
/// 底部菜单View 管理:目录,夜间,设置,缓存,反馈
protocol XYBottomMenuViewDelegate:NSObjectProtocol {
    /// 上一章
    func bottmMenuBefore(bookListView:XYBottomMenuView)
    
    /// 下一章
    func bottmMenuAfter(bookListView:XYBottomMenuView)

    /// 目录
    func bottmMenuChapterList(bookListView:XYBottomMenuView)
    
    /// 夜间
    func bottmMenuNight(bookListView:XYBottomMenuView)
    
    /// 设置
    func bottmMenuSetting(bookListView:XYBottomMenuView)
    
    /// 缓存
    func bottmMenuCache(bookListView:XYBottomMenuView)
    
    /// 反馈
    func bottmMenuConmmitBug(bookListView:XYBottomMenuView)
}


class XYBottomMenuView: UIView {

    /*
     290
     1.进度条 90
     2.上一章,下一章 120
     3.目录,夜间,设置,缓存,反馈 80
     */
   weak var delegate:XYBottomMenuViewDelegate?
    /// 进度条
    var slider = ASValueTrackingSlider()
    var beforeBtn = UIButton() // 上一章
    var afterBtn = UIButton() // 后一章
    var buttonsView = UIView()
    var btnArr:[UIButton] = [] // 装五个按钮
  
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    func setupUI() {
        backgroundColor = UIColor.black.withAlphaComponent(0.85)
        /// 进度条
        slider.setThumbImage(UIImage(named:"RM_3"), for: .normal)
        slider.minimumValue = 1
        slider.maximumValue = 10
        slider.setMaxFractionDigitsDisplayed(0)
        slider.popUpViewColor = UIColor.hm_color(withRed: 253, green: 85, blue: 84)
        slider.font = UIFont(name: "Helvetica-Bold", size: 22)
        slider.textColor = UIColor.white
        slider.popUpViewArrowLength = 10
        addSubview(slider)
        
        /// 上一章
        beforeBtn.setTitle("上一章", for: .normal)
        beforeBtn.setTitleColor(UIColor.white, for: .normal)
        beforeBtn.layer.borderColor = UIColor.white.cgColor
        beforeBtn.layer.borderWidth = 1
        beforeBtn.setTitleColor(UIColor.gray, for: .highlighted)
        beforeBtn.layer.cornerRadius = 6
        beforeBtn.addTarget(self, action: #selector(clickBefore), for: .touchUpInside)
        addSubview(beforeBtn)
        
        afterBtn.setTitle("下一章", for: .normal)
        afterBtn.setTitleColor(UIColor.white, for: .normal)
        afterBtn.layer.borderColor = UIColor.white.cgColor
        afterBtn.layer.borderWidth = 1
        afterBtn.layer.cornerRadius = 6
        afterBtn.setTitleColor(UIColor.gray, for: .highlighted)
        
        afterBtn.addTarget(self, action: #selector(clickAfter), for: .touchUpInside)
        addSubview(afterBtn)
         addSubview(buttonsView)
        let btnTitles:[String] = ["目录","夜间","设置","缓存","反馈"]
        let btnImageNames:[String] = ["directory","night_mode","stting_icon_3","preview_btn","feedback"]
        for index in 0...4 {
            let btn = UIButton()
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 12)
            btn.setTitle(btnTitles[index], for: .normal)
            btn.addTarget(self, action: #selector(clickButtomMenu(button:)), for: .touchUpInside)
            btn.setImage(UIImage.init(named: btnImageNames[index]), for: .normal)
            btnArr.append(btn)
            buttonsView.addSubview(btn)
        }
        
        XYLog(messsage: btnArr.count)
       
    }
    
    
    /// 上一章
    func clickBefore() {
        delegate?.bottmMenuBefore(bookListView: self)
    }
    
    
    /// 下一章
    func clickAfter() {
      delegate?.bottmMenuAfter(bookListView: self)
    }
    
    
    /// 点击菜单底部
    ///
    /// - Parameter button: 点击的按钮
    func clickButtomMenu(button:UIButton) {
        
        switch button.tag {
        case 0:
            delegate?.bottmMenuChapterList(bookListView: self)
            break
        case 1:
            delegate?.bottmMenuNight(bookListView: self)
            break
        case 2:
            delegate?.bottmMenuSetting(bookListView: self)
            break
        case 3:
            delegate?.bottmMenuCache(bookListView: self)
            break
        case 4:
            delegate?.bottmMenuConmmitBug(bookListView: self)
            break
        default:
            break
        }
    }
   
    
    override func layoutSubviews() {
        super.layoutSubviews()
        slider.frame = CGRect.init(x: 12, y: 12, width: ScreenWidth - 24, height: 44)
        
        beforeBtn.frame = CGRect.init(x: 12, y: slider.frame.maxY + 12, width: (ScreenWidth - 36) / 2, height: 30)
        afterBtn.frame = CGRect.init(x: beforeBtn.frame.maxX + 12, y: beforeBtn.y, width: beforeBtn.width, height: 30)
        
        let btnWidth = (ScreenWidth - 28) / 5
        for index in 0...btnArr.count - 1 {
            let btn = btnArr[index]
            btn.tag = index
            let btnX = 14 + (index * Int(btnWidth))
            btn.frame = CGRect.init(x: CGFloat(btnX), y: 8, width: btnWidth, height: 60)
            btn.layoutButtonEdgeInsets(style: .ButtonEdgeInsetsStyleTop, space: UIScreen.main.scale)
        }
        buttonsView.frame = CGRect.init(x: 0, y: afterBtn.frame.maxY, width: ScreenWidth, height: 90)
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
